<?php
/**
 * @Description 
 * @Sonkwl Xiong
 * @Date 2022/03/04 08:06:15
 */
namespace procjson;
class http{
    //@Sonkwl Xiong 2022/03/04 08:08:24
    //@Desc 获得请求，验证，转换成json
    static public function Request(){
        if($_SERVER["REQUEST_METHOD"]!="POST"){
            echo 'error 404';
            exit;
        }
        $command = isset($GLOBALS['HTTP_RAW_POST_DATA'])?$GLOBALS['HTTP_RAW_POST_DATA']:file_get_contents("php://input");
        $command=str_replace("'","''",trim($command,'"'));
        //$command=htmlspecialchars($command,ENT_NOQUOTES);//html转义
        $json=(array)json_decode($command,true);//true,转化成数组
        $json=self::FilterArray($json);
        if(count($json)==0){
            self::Response(array("res"=>"NG","message"=>"输入参数错误，必须是JSON格式"));
            exit;
        }
        return $json;
    }
    //@Sonkwl Xiong 2022/03/04 08:11:24
    //@Desc 响应
    static public function Response($res){
        $res=self::FilterArray($res);
        echo json_encode($res,JSON_UNESCAPED_UNICODE);
        exit;
    }
    //遍历array，过滤转义html
    static public function FilterArray($arr){
        foreach ($arr as $key => $value) {
            $k=htmlspecialchars($key,ENT_NOQUOTES);
            if(is_array($value)){
                //嵌套遍历arr
                $arr[$k]=self::FilterArray($value);
                continue;
            }
            $v=htmlspecialchars($value,ENT_NOQUOTES);
            $arr[$k]=$v;
        }
        return $arr;
    }
    //@Sonkwl Xiong 2022/03/04 08:53:05
    //@Desc 解析token
    static public function DecodeToken($token){
        if($tokenarray=\procjson\jwt::Check($token)){
            return \procjson\jwt::Get($tokenarray);
        }else{
            self::Response(array("res"=>"NG","message"=>"TOKEN無效或者已經過期"));
        }
    }
}
?>